package com.xyc.manager.permission.controller;


import com.xyc.manager.global.Message;
import com.xyc.manager.permission.bean.ParentTreeBean;
import com.xyc.manager.permission.service.PermissionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("user")
public class PermissonController {
    @Resource
    private PermissionService permissionService;

    @RequestMapping("login")
    @ResponseBody
    public Message login(String username, String password){
        Message message = new Message();
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
            SecurityUtils.getSubject().login(usernamePasswordToken);
            message.setCode(0);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            message.setCode(1);
            message.setMsg("用户名或者密码错误");
        }
        return message;
    }



    @RequestMapping("permission")
    @ResponseBody
    public List<ParentTreeBean> toPermissonPage(){
        String role = SecurityUtils.getSubject().getSession().getAttribute("role").toString();
        List<ParentTreeBean> parentTreeBeans = permissionService.queryParentTreeBeans(role);
        return parentTreeBeans;
    }
    /*@RequiresRoles("admin")
    @RequestMapping("all-permission")
    @ResponseBody
    public List<ParentTreeBean> queryAllAndHasPermission(String roleCode){
        List<ParentTreeBean> parentTreeBeanList = permissionService.queryRolePermissionByRoleCode(roleCode);
        return parentTreeBeanList;
    }*/
    @RequestMapping("del-permission")
    @ResponseBody
    public Message deletePermission(Integer fid, String roleId, String moduleCode){
        System.out.println(moduleCode);
        System.out.println(roleId);
        Integer fid1 = permissionService.queryFidByModuleCodeAndRoleId(roleId, moduleCode);
        //List<ParentTreeBean> parentTreeBeans = permissionService.queryRolePermissionByRoleCode("92122707");
        //for ()
        Message message = new Message();
        try {
            permissionService.cancelPermission(fid1,"92122707");
            message.setCode(0);
        } catch (Exception e) {
            e.printStackTrace();
            message.setCode(1);
        }

        return message;
    }
    @RequestMapping("add-permission")
    @ResponseBody
    public Message addPermission(String roleId,String moduleCode){
        //System.out.println(roleId);
       //System.out.println(moduleCode);
        Message message = new Message();
        try {
            permissionService.addPermission(roleId, moduleCode);
            message.setCode(0);
            message.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            message.setCode(1);
            message.setMsg("添加失败");
        }


        return message;
    }


}
